//
//  YZNetworkManager.m
//  YZStock
//
//  Created by fenchol on 15/7/7.
//  Copyright (c) 2015年 fenchol. All rights reserved.
//

#import "YZNetworkManager.h"
#import "YZRequestURLs.h"
//#import "NSError+YZM.h"
#import "YZMantleSerialization.h"

@implementation YZNetworkManager

+ (instancetype)sharedManager
{
    static YZNetworkManager *sInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sInstance = [[self alloc] init];
        sInstance.mainNetwork = [[YZHTTPRequestOperationManager alloc] initWithBaseURL: [NSURL URLWithString: URL_BASE_STRING]];
        sInstance.mainNetwork.requestSerializer.HTTPShouldHandleCookies = YES;
        sInstance.mainNetwork.requestSerializer.timeoutInterval = 10;
//        sInstance.mainNetwork.securityPolicy.allowInvalidCertificates = YES;
        
        NSDictionary *urlMap = YZ_URLModelMap();
        NSSet *acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain",@"text/html", nil];
        
        YZMantleSerialization *serialization = [YZMantleSerialization serializer];
        serialization.URLModelMap = urlMap;
        sInstance.mainNetwork.responseSerializer = serialization;
        sInstance.mainNetwork.responseSerializer.acceptableContentTypes = acceptableContentTypes;
        
        sInstance.quoteNetork = [[YZHTTPRequestOperationManager alloc] initWithBaseURL: [NSURL URLWithString: URL_BASE_STRING]];
        sInstance.quoteNetork.requestSerializer.HTTPShouldHandleCookies = YES;
        sInstance.quoteNetork.requestSerializer.timeoutInterval = 10.;
//        sInstance.quoteNetork.securityPolicy.allowInvalidCertificates = YES;
        
        serialization.URLModelMap = urlMap;
        sInstance.quoteNetork.responseSerializer = serialization;
        sInstance.quoteNetork.responseSerializer.acceptableContentTypes = acceptableContentTypes;
        
        //文件下载服务注册
        sInstance.downloadNetwork = [[YZHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:URL_PATCH_STRING]];
        
    });
    
    return sInstance;
}


#pragma mark - forward

- (id)forwardingTargetForSelector:(SEL)aSelector
{
    if ([self.mainNetwork respondsToSelector: aSelector]) {
        return self.mainNetwork;
    }
    
    return nil;
}

@end
